﻿using System;
using System.Drawing;

namespace Daco.Web.Task
{
    public class CUploadImageTask : Daco.Tasks.CLongBackgroundTask
    {
        private string _storePath;
        private string _productID;
        private string[] _files;
        private int _defIndex;

        public CUploadImageTask(string productID, string[] files)
            : base()
        {
            _files = new string[files.Length];
            _productID = productID;

            for (int i = 0; i < _files.Length; i++)
                _files[i] = files[i];
        }
        public CUploadImageTask(string productID, string[] files, int defIndex)
            : this(productID, files)
        {
            _defIndex = defIndex;
        }

        public CUploadImageTask(string productID, string[] files, string storePath)
            : this(productID, files)
        {
            _storePath = storePath;
        }
        public CUploadImageTask(string productID, string[] files, int defIndex, string storePath)
            : this(productID, files, defIndex)
        {
            _storePath = storePath;
        }
        public override void RunOnce()
        {

            for (int i = 0; i < _files.Length; i++)
            {
                string file = _files[i];
                if (System.IO.File.Exists(file))
                {
                    try
                    {
                        // Open image file
                        System.IO.FileStream imageStream = System.IO.File.Open(file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

                        byte[] imageContent = new byte[imageStream.Length];
                        imageStream.Read(imageContent, 0, imageContent.Length);

                        // Release resources
                        imageStream.Close();
                        imageStream.Dispose();

                        byte[] thumbImage = null;
                        byte[] largeImage = null;

                        // calculate image size
                        using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageContent)))
                        {
                            

                            if (oldImage.Size.Width > Config.CSiteConfig.Current.ImageSection.ThumbImageSize || oldImage.Size.Height > Config.CSiteConfig.Current.ImageSection.ThumbImageSize)
                            {
                                thumbImage = Daco.Utils.CHelper.ResizeImageFile(imageContent, Config.CSiteConfig.Current.ImageSection.ThumbImageSize);
                            }
                            else
                            {
                                thumbImage = imageContent;
                            }

                            if (oldImage.Size.Width > Config.CSiteConfig.Current.ImageSection.LargeImageSize || oldImage.Size.Height > Config.CSiteConfig.Current.ImageSection.LargeImageSize)
                            {
                                largeImage = Daco.Utils.CHelper.ResizeImageFile(imageContent, Config.CSiteConfig.Current.ImageSection.LargeImageSize);
                            }
                            else
                            {
                                largeImage = imageContent;
                            }
                        }

                        if (string.IsNullOrEmpty(_storePath))
                        {
                            string fname = System.IO.Path.GetFileName(file);
                            // store in db
                            //Daco.Web.Store.CProduct.AddProductPhoto
                            //    (
                            //        _productID,
                            //        Config.CSiteConfig.Current.ImageSection.PrefixStringForThumbImage + fname,
                            //        thumbImage,
                            //        fname,
                            //        largeImage,
                            //        i == _defIndex ? true : false
                            //    );
                        }
                        else
                        {
                            string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(file);
                            string absolutePath = CPageContext.Context.Server.MapPath(_storePath);
                            
                            // save thumb
                            Daco.Utils.CHelper.SaveImage(
                                thumbImage, 
                                absolutePath + Config.CSiteConfig.Current.ImageSection.PrefixStringForThumbImage + fileNameWithoutExtension, 
                                Config.CSiteConfig.Current.ImageSection.ImageStoreFormat);

                            // save large
                            Daco.Utils.CHelper.SaveImage(
                                largeImage,
                                absolutePath + fileNameWithoutExtension,
                                Config.CSiteConfig.Current.ImageSection.ImageStoreFormat);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
        }
    }
}
